home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / Velocity Engine / VelEng Wavelet / source / CBasicApp.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  10.9 KB  |  381 lines  |  [TEXT/CWIE]

  1. //    This file contains the starter code for a basic PowerPlant project
  2. /*
  3.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  4.                 ("Apple") in consideration of your agreement to the following terms, and your
  5.                 use, installation, modification or redistribution of this Apple software
  6.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  7.                 please do not use, install, modify or redistribute this Apple software.
  8.  
  9.                 In consideration of your agreement to abide by the following terms, and subject
  10.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  11.                 copyrights in this original Apple software (the "Apple Software"), to use,
  12.                 reproduce, modify and redistribute the Apple Software, with or without
  13.                 modifications, in source and/or binary forms; provided that if you redistribute
  14.                 the Apple Software in its entirety and without modifications, you must retain
  15.                 this notice and the following text and disclaimers in all such redistributions of
  16.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  17.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  18.                 Apple Software without specific prior written permission from Apple.  Except as
  19.                 expressly stated in this notice, no other rights or licenses, express or implied,
  20.                 are granted by Apple herein, including but not limited to any patent rights that
  21.                 may be infringed by your derivative works or by other works in which the Apple
  22.                 Software may be incorporated.
  23.  
  24.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  25.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  26.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  28.                 COMBINATION WITH YOUR PRODUCTS.
  29.  
  30.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  31.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  34.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  35.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  36.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38.  
  39. #include "CBasicApp.h"
  40.  
  41. #include <LGrowZone.h>
  42. #include <PP_Messages.h>
  43. #include <PP_Resources.h>
  44. #include <UDrawingState.h>
  45. #include <UMemoryMgr.h>
  46. #include <URegistrar.h>
  47. #include <UNavServicesDialogs.h>
  48. #include <LGAProgressBarImp.h>
  49.  
  50. #include <LWindow.h>
  51. #include <LCaption.h>
  52. #include "CTransformPane.h"
  53. #include <LProgressBar.h>
  54.  
  55. #include "sWavelet_int.h"
  56.  
  57. #include "QTReadWriteJPEG.h"
  58.  
  59. #define WAVELET_32_RAW_WIDTH    640
  60. #define WAVELET_32_RAW_HEIGHT    480
  61.  
  62. #define SUBTRACT_SHIFT            0
  63. #define DO_TT6_TRACE            1
  64.  
  65.  
  66. const PP_PowerPlant::ResIDT    wind_SampleWindow = 128;    // EXAMPLE, create a new window
  67.  
  68.  
  69. // ===========================================================================
  70. //        • Main Program
  71. // ===========================================================================
  72.  
  73.  
  74. #define SMALLTEST_TERM        (32*i+31)
  75.  
  76. #define ARRAYSIDE     64
  77. #define OVERFLOW    8
  78.  
  79. #define BLOCKBYTES    (ARRAYSIDE*ARRAYSIDE*4*sizeof(short))
  80.  
  81.  
  82.  
  83. #define COMPARE_DIMENSION    16
  84. #define ARRAYELEM            i
  85.  
  86.  
  87. int main()
  88. {        
  89.     SetDebugThrow_(PP_PowerPlant::debugAction_Alert);    // Set Debugging options
  90.     SetDebugSignal_(PP_PowerPlant::debugAction_Alert);
  91.  
  92.     PP_PowerPlant::InitializeHeap(3);                    // Initialize Memory Manager
  93.                                                         // Parameter is number of Master Pointer
  94.                                                         // blocks to allocate
  95.     
  96.     PP_PowerPlant::UQDGlobals::InitializeToolbox(&qd);    // Initialize standard Toolbox managers
  97.     
  98.     new PP_PowerPlant::LGrowZone(20000);                // Install a GrowZone function to catch
  99.                                                         // low memory situations.
  100.  
  101.     CBasicApp    theApp;                                    // replace this with your App type
  102.     theApp.Run();
  103.     
  104.     return 0;
  105. }
  106.  
  107.  
  108. // ---------------------------------------------------------------------------
  109. //        • CBasicApp
  110. // ---------------------------------------------------------------------------
  111. //    Constructor
  112.  
  113. CBasicApp::CBasicApp()
  114. {
  115.     RegisterClass_(PP_PowerPlant::LWindow);        // You must register each kind of
  116.     RegisterClass_(PP_PowerPlant::LCaption);    // PowerPlant classes that you use
  117.                                                 // in your PPob resource.
  118.     RegisterClass_(CTransformPane);
  119.     RegisterClass_(CCopyBitsPane);
  120.     RegisterClass_(LTextEditView);
  121.     
  122.     RegisterClass_(LCaption);
  123.     RegisterClass_(LProgressBar);
  124.     RegisterClass_(LGAProgressBarImp);
  125.     RegisterClass_(LDialogBox);
  126.     
  127.     
  128.     RegisterClass_(LCaption);
  129.     RegisterClass_(LEditField);
  130.     RegisterClass_(LPane);
  131.     RegisterClass_(LScroller);
  132.     RegisterClass_(LStdControl);
  133.     RegisterClass_(LStdButton);
  134.     RegisterClass_(LStdCheckBox);
  135.     RegisterClass_(LStdRadioButton);
  136.     RegisterClass_(LStdPopupMenu);
  137.     RegisterClass_(LTextEditView);
  138.     RegisterClass_(LView);
  139.     RegisterClass_(LWindow);    
  140.     
  141. }
  142.  
  143.  
  144. // ---------------------------------------------------------------------------
  145. //        • ~CBasicApp
  146. // ---------------------------------------------------------------------------
  147. //    Destructor
  148.  
  149. CBasicApp::~CBasicApp()
  150. {
  151. }
  152.  
  153. // ---------------------------------------------------------------------------
  154. //        • StartUp
  155. // ---------------------------------------------------------------------------
  156. //    This method lets you do something when the application starts up
  157. //    without a document. For example, you could issue your own new command.
  158.  
  159. void
  160. CBasicApp::StartUp()
  161. {
  162.     ObeyCommand(PP_PowerPlant::cmd_Open, nil);        // EXAMPLE, create a new window
  163. }
  164.  
  165. // ---------------------------------------------------------------------------
  166. //        • ObeyCommand
  167. // ---------------------------------------------------------------------------
  168. //    This method lets the application respond to commands like Menu commands
  169.  
  170. Boolean
  171. CBasicApp::ObeyCommand(
  172.     PP_PowerPlant::CommandT    inCommand,
  173.     void                    *ioParam)
  174. {
  175.     Boolean        cmdHandled = true;
  176.  
  177.     switch (inCommand) {
  178.  
  179.         case cmd_openRaw:
  180.             DoNew32Bit();
  181.             break;
  182.         
  183.     
  184.         // Handle command messages (defined in PP_Messages.h).
  185.         case PP_PowerPlant::cmd_Open:
  186. #if 1
  187.             DoNewJPEG();
  188.             break;
  189. #else                                        
  190.             PP_PowerPlant::LWindow    *theWindow =
  191.                                     PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this);
  192.             ThrowIfNil_(theWindow);
  193.             
  194.             // LWindow is not initially visible in PPob resource
  195.             theWindow->Show();
  196.             break;
  197. #endif
  198.         // Any that you don't handle, such as cmd_About and cmd_Quit,
  199.         // will be passed up to LApplication
  200.         default:
  201.             cmdHandled = PP_PowerPlant::LApplication::ObeyCommand(inCommand, ioParam);
  202.             break;
  203.     }
  204.     
  205.     return cmdHandled;
  206. }
  207.  
  208. // ---------------------------------------------------------------------------
  209. //        • FindCommandStatus
  210. // ---------------------------------------------------------------------------
  211. //    This method enables menu items.
  212.  
  213. void
  214. CBasicApp::FindCommandStatus(
  215.     PP_PowerPlant::CommandT    inCommand,
  216.     Boolean                    &outEnabled,
  217.     Boolean                    &outUsesMark,
  218.     PP_PowerPlant::Char16    &outMark,
  219.     Str255                    outName)
  220. {
  221.  
  222.     switch (inCommand) {
  223.     
  224.         // Return menu item status according to command messages.
  225.         case PP_PowerPlant::cmd_Open:
  226.         case cmd_openRaw:
  227.             outEnabled = true;
  228.             break;
  229.  
  230.         // Any that you don't handle, such as cmd_About and cmd_Quit,
  231.         // will be passed up to LApplication
  232.         default:
  233.             LApplication::FindCommandStatus(inCommand, outEnabled,
  234.                                                 outUsesMark, outMark, outName);
  235.             break;
  236.     }
  237. }
  238.  
  239.  
  240. void CBasicApp::DoNewJPEG()
  241. {
  242.     UNavServicesDialogs::LFileChooser        fileChooser;
  243.     OSType                                    types[] = {kQTFileTypeJPEG};
  244.     LFileTypeList                            typelist(2, types);
  245.     FSSpec                                    filespec;
  246.     GWorldPtr                                theGWorld = nil;
  247.     long                                    imageBytes;
  248.     long                                    width, height;
  249.     long                                    paddedWidth, paddedHeight;
  250.     long                                    requiredWaveletMultiple;
  251.     
  252.     if (fileChooser.AskChooseOneFile(typelist, filespec)) {
  253.     
  254.         FailOSErr_(QTJPEG_ReadJPEG (filespec, &theGWorld));
  255.     
  256.         width = theGWorld->portRect.right-theGWorld->portRect.left;
  257.         height = theGWorld->portRect.bottom-theGWorld->portRect.top;
  258.         
  259.         imageBytes =     (height) * (((**(theGWorld->portPixMap)).rowBytes) & 0x7fff) ;
  260.         
  261.         StHandleBlock        pixHandle(4);
  262.         
  263.         PixMapHandle        gworldPixMap = GetGWorldPixMap(theGWorld);
  264.  
  265.         LockPixels(gworldPixMap);
  266.         
  267.         Ptr    pixbase = GetPixBaseAddr(gworldPixMap);        
  268.  
  269.         requiredWaveletMultiple = 4 << 3;        // 8 shifted by wavelet depth
  270.  
  271.         paddedWidth = ((width + (requiredWaveletMultiple-1)) / requiredWaveletMultiple) * requiredWaveletMultiple;
  272.         paddedHeight = ((height + (requiredWaveletMultiple-1)) / requiredWaveletMultiple) * requiredWaveletMultiple;
  273.  
  274.         PadXY_32Bit((unsigned long*)pixbase, pixHandle, (**theGWorld->portPixMap).rowBytes & 0x7fff, width, height, paddedWidth, paddedHeight);
  275.         
  276.         PixMapHandle pmh = NewPixMap();
  277.         
  278.         CopyPixMap(theGWorld->portPixMap, pmh);
  279.         
  280.         CTabHandle saveCT = (**pmh).pmTable;
  281.         (**pmh).pmTable = nil;
  282.         
  283.         NewWindowWithData(**pmh, pixHandle, paddedWidth, paddedHeight);
  284.         
  285.         (**pmh).pmTable = saveCT;
  286.         
  287.         DisposePixMap(pmh);
  288.         
  289.         pixHandle.Release();    // we no longer own it
  290.         
  291.         DisposeGWorld(theGWorld);
  292.     }
  293. }
  294.  
  295.  
  296.  
  297. void CBasicApp::DoNew32Bit()
  298. {
  299.     UNavServicesDialogs::LFileChooser        fileChooser;
  300.     OSType                                    types[] = {'BINA', '????'};
  301.     LFileTypeList                            typelist(2, types);
  302.     FSSpec                                    filespec;
  303.  
  304.     if (fileChooser.AskChooseOneFile(typelist, filespec)) {
  305.         LFile    datafile(filespec);
  306.                         
  307.         PixMap    map;
  308.         
  309.         map.baseAddr = nil;
  310.         map.rowBytes = 0x8000 | (WAVELET_32_RAW_WIDTH*4);
  311.         map.bounds.top = 0;
  312.         map.bounds.bottom = WAVELET_32_RAW_HEIGHT;
  313.         map.bounds.left = 0;
  314.         map.bounds.right = WAVELET_32_RAW_WIDTH;
  315.         map.pmVersion = 4;
  316.         map.packType = 0;
  317.         map.packSize = 0;
  318.         map.hRes = 0x00480000;
  319.         map.vRes = 0x00480000;
  320.         map.pixelType = RGBDirect;
  321.         map.pixelSize = 32;
  322.         map.cmpCount = 3;
  323.         map.cmpSize = 8;
  324.         map.planeBytes = 0;
  325.         map.pmTable = nil;
  326.         map.pmReserved = 0;
  327.  
  328.         {
  329.             StHandleBlock    datahandle;
  330.  
  331.             datafile.OpenDataFork(fsRdPerm);
  332.             datahandle.Adopt(datafile.ReadDataFork());
  333.             
  334.             map.rowBytes = 0x8000 | (WAVELET_32_RAW_WIDTH+16)*4;
  335.             
  336.             NewWindowWithData(map, datahandle, WAVELET_32_RAW_WIDTH, WAVELET_32_RAW_HEIGHT);
  337.             
  338.             datahandle.Release();    // we no longer own it, the pane does
  339.         }        
  340.  
  341.         
  342.     }
  343.  
  344. }
  345.  
  346.  
  347. void CBasicApp::NewWindowWithData(const PixMap &pmap, Handle data, long paddedX, long paddedY)
  348. {
  349.     long        width = pmap.bounds.right-pmap.bounds.left;
  350.     long        height = pmap.bounds.bottom-pmap.bounds.top;
  351.  
  352.     LWindow        *pWin = PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this);
  353.     ThrowIfNil_(pWin);
  354.     
  355.     CCopyBitsPane* pBitsPane = dynamic_cast<CCopyBitsPane*>(pWin->FindPaneByID('BMap'));
  356.     FailNIL_(pBitsPane);
  357.     
  358.     pBitsPane->SetPixelData(pmap, data, paddedX, paddedY);
  359.  
  360.     LCommander::SwitchTarget(pBitsPane);
  361.  
  362.     pWin->ResizeWindowTo(width, height);
  363.  
  364.     // LWindow is not initially visible in PPob resource
  365.     pWin->Show();
  366.     
  367. }
  368.  
  369.  
  370. void
  371.  CBasicApp::ShowAboutBox()
  372. {
  373.     PP_PowerPlant::LWindow    *theWindow =
  374.                             PP_PowerPlant::LWindow::CreateWindow(1102, this);
  375.     ThrowIfNil_(theWindow);
  376.     
  377.     // LWindow is not initially visible in PPob resource
  378.     theWindow->Show();
  379. }
  380.  
  381.